 #!/usr/bin/python
 # -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
import os, sys
reload(sys)
sys.setdefaultencoding('utf-8')
import traceback

def getReviewerWhen(review):

    header = review.find('div',{'class': 'header'})
    reviewer = header.find('div',{'class':'reviewer'})
    reviewer = reviewer.find('a')['href'].split('/')[2]
    when = header.find('div',{'class':'posted_time'}).time.text

    r_info = {}
    r_info['reviewerName'] = reviewer
    r_info['reviewTime'] = when

    return r_info

def get_replyCmts(cmt, request_id):
    try:
        reply = cmt.find('ol',{'class': 'reply-comments'})
        if reply:
            role = "replyer"
            dts = reply.findAll('dt')
            dds = reply.findAll('dd')
            for i in range(0,len(dts)):
                dt = dts[i]
                dd = dds[i]
                replyer = dt.find('div',\
                        {'class':'user-reply-info'}).a['href'].split('/')[2]

                replyTime = dt.find('span',{'class':'timestamp'}).time.text
                replyCmt = dd.find('pre', \
                        {'class': 'reviewtext rich-text'})\
                        .text.strip().replace('\n', u'去')
                print '邹'.join([request_id, role, replyer, replyTime, replyCmt])
    except:
        traceback.print_exc()

def getTopComments(review, request_id):

    r_info = getReviewerWhen(review)
    body = review.find('div',{'class': 'body'})
    topr = body.find('pre',{'class': 'body_top reviewtext rich-text'})
    if topr is not None:
        topC = topr.text.strip()
        if topC != '':
            role = 'reviewer'
            topC = topC.replace('\n',u'去')
            reviewer = r_info['reviewerName']
            when = r_info['reviewTime']
            print '邹'.join([request_id,role,reviewer,when,topC])

    cmtOnTop = body.find('div',{'class': 'body-top-comment-section'})
    if cmtOnTop:
        get_replyCmts(cmtOnTop, request_id)


def getInlineComments(review, request_id):

    r_info = getReviewerWhen(review)
    body = review.find('div',{'class': 'body'})
    dl = body.find('dl', {'class': 'review-comments'})
    if dl:
        dds = body.findAll('dd')
        for dd in dds:
            cmt = dd.find('pre',{'class': 'reviewtext comment-text rich-text'})
            if cmt:
                cmttext = cmt.text.strip()
                if cmttext != '':
                    cmttext=cmttext.replace('\n', u'去')
                    role = 'reviewer'
                    reviewer = r_info['reviewerName']
                    when = r_info['reviewTime']
                    print '邹'.join([request_id,role,reviewer,when,cmttext])

            replycmts = dd.find('div', {'class': 'comment-section'})
            if replycmts:
                 get_replyCmts(replycmts, request_id)

    '''if inlineOrTop:
    =0, then retrieve both top and inline comments
    =1, then only top comments,
    =2, then inline comments
    '''
def get_review_comments(request_html, inlineOrTop, submitter):
    #request_id format:path/id.html. e.g., reviews2017/10000.html
    request_id = request_html.strip().split('/')[-1].split('.')[0]
    rhtml=open(request_html,'r')
    soup = BeautifulSoup(rhtml, 'lxml')
    reviewList=soup.findAll('div',{'class': 'review'})

    robots = ['mesos-review','aurorabot','asfbot','mesos-review-windows']
    for r in reviewList:
        try:
            r_info = getReviewerWhen(r)
            reviewer = r_info['reviewerName']
            '''we need to filter out the comments made by submitter themselves.
               also, reviewbot comments should also be removed out.
            '''
            if (reviewer not in robots) and (reviewer != submitter):
                if inlineOrTop == 1:
                    getTopComments(r, request_id)
                elif inlineOrTop == 2:
                    getInlineComments(r, request_id)
                elif inlineOrTop == 0:
                    getTopComments(r, request_id)
                    getInlineComments(r, request_id)
            elif reviewer in robots:
                print>>file('robots.res','a'), \
                        request_id, reviewer, submitter
            elif reviewer == submitter:
                print>>file('submitterReview.res','a'), \
                        request_id, reviewer, submitter
        except:
            traceback.print_exc()

if __name__ == '__main__':
    #request_html="64417.html"#sys.argv[1]
    #request_html="18945.html"#sys.argv[1]
    #request_html="6302.html"#sys.argv[1]
    request_html=sys.argv[1]
    #if inlineOrTop:
    #=0, then both top and inline comments
    #=1, only top comments,
    #=2, only inline comments
    inlineOrTop = int(sys.argv[2])
    #we should filter out submitter's reviews
    submitter = sys.argv[3]
    get_review_comments(request_html, inlineOrTop, submitter)
